package com.project.forlearning.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.forlearning.service.BackResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 资源处理API
 */
@Api(value = "BackResourceController 后台需要的关于资源处理API",tags = {"后台关于资源的API"})
@RestController
@RequestMapping("resources")
@CrossOrigin
public class BackResourceController {

    @Autowired
    private BackResourceService backResourceService;

    @ApiOperation("查询单个资源，需传入resourceId")
    @GetMapping("{resourceId}")
    public String getResource(@ApiParam("资源编号 resourceId") @PathVariable int resourceId){
        return backResourceService.findResource(resourceId, null);
    }

    @ApiOperation("查询所有资源")
    @GetMapping()
    public String getAllResource(){
        return backResourceService.findResource(null, null);
    }

    @ApiOperation("查询审核中状态的资源 （状态分为三种： 审核中，拒绝，通过）")
    @GetMapping("status")
    public String getStatusResource(@ApiParam("资源状态") @RequestParam("status") String status){
        return backResourceService.findResource(null, status);
    }

    @ApiOperation("删除单个资源，需传入resourceId")
    @DeleteMapping("{resourceId}")
    public String deleteUserResource(@ApiParam("资源编号 resourceId") @PathVariable int resourceId){
        return backResourceService.deleteResource(resourceId, null);
    }

    @ApiOperation("删除某个状态的资源，需传入status")
    @DeleteMapping("status")
    public String deleteUserResource(@ApiParam("资源状态") @RequestParam("status") String status){
        return backResourceService.deleteResource(null, status);
    }

    @ApiOperation("删除全部资源 尽量别用")
    @DeleteMapping("/all")
    public String deleteUserResourceAll(){
        return backResourceService.deleteResource(null, null);
    }

    @ApiOperation("增加单个资源")
    @PostMapping("{userId}")
    public String addResource(
         @ApiParam(value = "标题 （资源名称）必填", required = true) @RequestParam("title") String title,
         @ApiParam(value = "描述 必填", required = true) @RequestParam("introduction") String introduction,
         @ApiParam(value = "资源地址 必填",required = true) @RequestParam("url") String url,
         @ApiParam(value = "标签 必填",required = true) @RequestParam("labels") String labels,
         @ApiParam("图标") @RequestParam(value = "icon", required = false, defaultValue = "") String icon,
         @ApiParam("详情") @RequestParam(value = "details", required = false, defaultValue = "") String details,
         @ApiParam(value = "发布者 必填", required = true) @PathVariable("userId") int userId,
         @ApiParam(value = "资源状态", required = false, defaultValue = "通过") @RequestParam(value = "status", required = false, defaultValue = "通过") String status
    ){
        return backResourceService.addResource(title, introduction, url, labels, icon, details, userId, status);
    }

    @ApiOperation("修改单个资源 请与resource字段对应")
    @PutMapping("{resourceId}")
    public String updateResource(@ApiParam(value = "资源编号") @PathVariable int resourceId,
                                 @ApiParam(value = "资源")@RequestBody String resource
                                ){
        JSONObject jsonObject = JSONObject.parseObject(resource);
        String title = (String) jsonObject.get("title");
        String introduction = (String) jsonObject.get("introduction"); 
        String url = (String) jsonObject.get("url");
        String labels = (String) jsonObject.get("labels");
        String icon = (String) jsonObject.get("icon");
        String details = (String) jsonObject.get("details");
        Integer collectionNum = (Integer) jsonObject.get("collectionNum");
        Integer scanNum = (Integer) jsonObject.get("scanNum");
        Integer starNum = (Integer) jsonObject.get("starNum");
        String status = (String) jsonObject.get("status");

        return backResourceService.updateResource(resourceId, title, introduction, url, labels, icon, details, collectionNum, scanNum, starNum, status);
    }

    @ApiOperation("设置资源状态")
    @PutMapping("setStatus/{resourceId}")
    public String rejectResource(@ApiParam("资源编号") @PathVariable int resourceId,
                                 @ApiParam("资源状态") @RequestParam("status") String status){
        return backResourceService.updateResource(resourceId, null, null, null, null, null, null, null, null, null, status);
    }
}
